<?php
defined( 'JOSCOM_VERSION' ) or die( 'Direct Access to this location is not allowed.' );
  if (!($acl->acl_check( 'administration', 'edit', 'users', $my->usertype, 'components', 'all' )
  | $acl->acl_check( 'administration', 'edit', 'users', $my->usertype, 'components', 'com_banners' ))) {
    //j15nc
  }
  if (defined( '_OSCOMMERCE_LOADED' )) {
    if (!DB_SHARED) {
      tep_db_connect() or die('Unable to connect to database server!');
    }
    global $category_links, $languages_id, $cPath, $cPath_array, $current_category_id;
    global $request_type, $lng, $cart, $currencies, $language, $navigation, $breadcrumb;
  } else {
    require(DIR_WS_COMPONENT . 'includes/application_top.php');
  }
  require(DIR_WS_COMPONENT . 'includes/filenames.php');
  require(DIR_WS_COMPONENT . 'includes/functions/verify_language.php');

  $language_copy_order_array = array("english","german","espanol","tchinese","chinese","french"); 
 
  $languages_folder_array = array();

$extension_array = array('image' => array("gif", "jpg", "png", "bmp", "tif","jpeg"),
						'php' => array("php"));
/*
 * Folder array for the pull down menu
 */
$folder_array = array(0=>array('id' => '', 'text' => ''),
					1=>array('id' => 'admin', 'text' => 'admin'),
					2=>array('id' => 'catalog', 'text' => 'catalog'));
					//3=>array('id' => 'xoops', 'text' => 'xoops'));

$default_language_pair_array = array("tchinese" => "en|zh-TW",
									 "chinese" => "en|zh-TW",
									 "french" => "en|fr",
									 "german" => "en|de",
									 "english" => "en|es",
									 "espanol" => "en|es",
									 "italian" => "en|it",
									 "japanese" => "en|ja",
									 "portugues" => "en|pt"	);					
					
					
/*
 * Get all the laguage pairs that are available through Google Translate
 * This array may update as google brings on more languages
 */				
$google_language_pair_array = array(0=> array("id" => "", "text" => "Please Choose"),									
									1=> array("id" => "ar|en", "text" => "Arabic to English"),
									2=> array("id" => "zh|en" ,"text" => "Chinese to English"),
									3=> array("id" => "zh-CN|zh-TW", "text" => "Chinese (Simplified to Traditional)"),
									4=> array("id" => "zh-TW|zh-CN", "text" => "Chinese (Traditional to Simplified)"),
									5=> array("id" => "nl|en" ,"text" => "Dutch to English"),
									6=> array("id" => "en|ar" ,"text" => "English to Arabic"),
									7=> array("id" => "en|zh-CN", "text" => "English to Chinese (Simplified)"),
									8=> array("id" => "en|zh-TW", "text" => "English to Chinese (Traditional)"),
									9=> array("id" => "en|nl", "text" => "English to Dutch"),
									10=> array("id" => "en|fr", "text" => "English to French"),
									11=> array("id" => "en|de", "text" => "English to German"),
									12=> array("id" => "en|el", "text" => "English to Greek"),
									13=> array("id" => "en|it", "text" => "English to Italian"),
									14=> array("id" => "en|ja", "text" => "English to Japanese"),
									15=> array("id" => "en|ko", "text" => "English to Korean"),
									16=> array("id" => "en|pt", "text" => "English to Portuguese"),
									17=> array("id" => "en|ru", "text" => "English to Russian"),
									18=> array("id" => "en|es", "text" => "English to Spanish"),
									19=> array("id" => "fr|en" ,"text" => "French to English"),
									20=> array("id" => "fr|de", "text" => "French to German"),
									21=> array("id" => "de|en" ,"text" => "German to English"),
									22=> array("id" => "de|fr", "text" => "German to French"),
									23=> array("id" => "el|en" ,"text" => "Greek to English"),
									24=> array("id" => "it|en", "text" => "Italian to English"),
									25=> array("id" => "ja|en", "text" => "Japanese to English"),
									26=> array("id" => "ko|en", "text" => "Korean to English"),
									27=> array("id" => "pt|en", "text" => "Portuguese to English"),
									28=> array("id" => "ru|en", "text" => "Russian to English"),
									29=> array("id" => "es|en", "text" => "Spanish to English")); 
									  
/*
 * If we are dealing with a single filename, we will get all of the varialble
 * if the filename is empty we build a page with all the files available
 */
  
if($_GET['filename']){
	$filename = $_GET['filename'];
	$file_extension = findexts($_GET['filename']);
} else {
	$filename = '';
}



if($_GET['directory'] == 'admin'){
		$directory = DIR_WS_LANGUAGES;
		$directory_desc = $_GET['directory'];
} elseif ($_GET['directory'] == 'catalog'){
		$directory = DIR_FS_CATALOG_LANGUAGES;
		$directory_desc = $_GET['directory'];
} elseif ($_GET['directory'] == 'xoops'){
		$directory = DIR_FS_XOOPS_LANGUAGES;
		$directory_desc = $_GET['directory'];
} else {
	$directory_desc = 'catalog';
	$directory = DIR_FS_CATALOG_LANGUAGES;
}

/*
 * Copy a file that does not exist to a language folder.
 * Base the file on another one shich exists
 */ 

if(($_GET['action'] == 'copy') && ($filename)  && ($_GET['lang']) && ($_GET['path'])){
	$language_copy_to = $_GET['lang'];
	$path = $_GET['path'];
	$path_copy_to = str_replace("english", $language_copy_to, $path);

	if(!file_exists($path_copy_to.$filename)){
		copy($path.$filename,$path_copy_to.$filename);
		
	}
}

  

/*
 * Backup all the language files - just to be safe.
 * Should be done BEFORE you begin translating.
 */									
if($_GET['action'] == 'backup'){
	$backup_directory = trim($directory,"/");
	$backup_directory .= "_backup/";
	full_copy($directory,$backup_directory);
}
					

					
global $database;
$database->setQuery("Select directory from #__osc_languages");
$result = $database->loadObjectList();
$numrows = count($result);
 



$languages_array = array();


foreach($result as $resultset){
array_push($languages_array, $resultset->directory);
}

if (isset($_GET['ext'])){
	$file_ext = $extension_array[$_GET['ext']];
	$query_string_ext = '&ext='.$_GET['ext'];
} else {
	$file_ext = $extension_array['php'];
}




$files = filelist($filename,$directory,1,0,'all',1,$extension_array['php']);




$files_language= array();

foreach ($files as $list) {
    //Builds the languages the files is available in
	if ($list['dir'] == 0){
    	//echo "Directory: " . $list['dir'] . " => Level: " . $list['level'] . " => Language: " . $list['language'] . " => Name: " . $list['name'] . " => Path: " . $list['path'] ." => Type: " . $list['type'] ."<br>";
    	$files_language[$list['name']]['path'][$list['language']]= $list['path'];
    	$files_language[$list['name']][$list['language']] = count($list['fields']);
    	
    	while(list($fieldname,$fieldtext) = each($list['fields'])){
    		$files_language[$list['name']]['fields'][$fieldname][$list['language']] = $fieldtext;
    	}
    }
	//Builds the Languages from the folder names	
	if (($list['level'] == 1) && ($list['dir']==1)) {
		$listname = $list['name'];
		array_push($languages_folder_array,$listname);
	}
} 


?>

<link rel="stylesheet" type="text/css" href="<?php echo DIR_WS_COMPONENT ?>includes/stylesheet.css">
<script language="javascript"><!--
function popupImageWindow(url) {
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=600,height=300,screenX=150,screenY=150,top=150,left=150')
}

function toggle(id) {
	var e = document.getElementById(id);
	if(e.style.display == 'none')
	e.style.display = 'block';
	else
	e.style.display = 'none';
}

//-->
</script>
<script type="text/javascript">
var http = false;

if(navigator.appName == "Microsoft Internet Explorer") {
	http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
	http = new XMLHttpRequest();
}

function updateText(field,file,language,directory) {
                         
	document.getElementById(field+'_'+language+'_save').style.display = "none";
	document.getElementById(field+'_'+language+'_anim').style.display = "inline";
	var text_value = document.getElementById(field+'_'+language+'_text').value;
	
	http.abort();
	var url ='index3.php?option=com_oscommerce&osMod=ajax_rewrite_fields&field='+field+'&file='+file+'&lang='+language+'&text='+encodeURI(text_value)+'&directory='+directory;
	http.open("GET", url, true);

	http.onreadystatechange=function() {
		if(http.readyState == 4) {
			if(http.responseText){
				document.getElementById(field+'_'+language+'_anim').style.display = "none";
				document.getElementById(field+'_'+language+'_save').style.display ="inline";
				if(http.responseText.indexOf('error')==-1){
						document.getElementById(field+'_'+language+'_top').innerHTML =http.responseText;
						document.getElementById(field+'_'+language).style.display = "none";
				}else{
						alert(http.responseText);
				}
			}
		}
	}
	http.send(null);
}

function getTranslation(field,language,language_pair) {
                         
	document.getElementById(field+'_'+language+'_save').style.display = "none";
	document.getElementById(field+'_'+language+'_anim').style.display = "inline";
	var text_value = document.getElementById(field+'_'+language+'_text').value;
	var language_pair = document.getElementById(field+'_'+language+'_select').value;
                  	
	http.abort();
	var url ='index3.php?option=com_oscommerce&osMod=ajax_google_translate&text='+text_value+'&language_pair='+language_pair;
	http.open("GET", url, true);

	http.onreadystatechange=function() {
		if(http.readyState == 4) {
			if(http.responseText){
				document.getElementById(field+'_'+language+'_anim').style.display = "none";
				document.getElementById(field+'_'+language+'_save').style.display ="inline";
				if(http.responseText.indexOf('error')==-1){
					document.getElementById(field+'_'+language+'_text').value = http.responseText;
				}else{
					alert(http.responseText);
				}
			}
		}
	}
	http.send(null);
}

function alert_function(field,language,language_pair){
	alert(field+' '+language+' '+language_pair);
}

</script>


<script language="javascript"><!--
function popupWindow(url) {
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=600,height=300,screenX=150,screenY=150,top=150,left=150')
}
//--></script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<div id="spiffycalendar" class="text"></div>
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr>
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1" cellpadding="1" class="columnLeft">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
    </table></td>
<!-- body_text //-->
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
            <td><?php echo tep_draw_separator('pixel_trans.gif', HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?></td>
            <td class="pageHeading" align="right"><?php echo TEXT_LANGUAGE_FOLDER_TO_CRAWL." ".tep_draw_form('languagefolder', tep_href_link(FILENAME_VERIFY_LANGUAGE, ''), 'get') . tep_draw_pull_down_menu('directory', $folder_array, '', 'onChange="this.form.submit();"') . tep_hide_session_id() . '</form>'; ?>
            </td>
          </tr>
          <tr>
          	<td colspan=2>
          		<b><?php echo TEXT_DIRECTORY; ?></b><?php echo "<b>".$_GET['directory']."</b> <span class='dataTableContent'>(".$directory.")</span>"; ?>
          	</td>
          	<td align="right">
          		&nbsp;&nbsp;&nbsp;<a href="<?php echo tep_href_link(FILENAME_VERIFY_LANGUAGE,"directory=".$directory_desc."&action=backup"); ?>"><?php echo TEXT_BACKUP_LANGUAGE_FILES; ?></a>
          	</td>
          </tr>
          <tr>
            <td colspan=2>
            &nbsp;&nbsp;&nbsp;<a href="<?php echo tep_href_link(FILENAME_VERIFY_LANGUAGE_IMAGES,"directory=".$directory_desc); ?>"><?php echo TEXT_IMAGE_FILES; ?></a>
            </td>
            <td class="pageHeading" align="right"><?php echo tep_draw_form('adminlanguage', FILENAME_DEFAULT, '', 'get') . tep_draw_pull_down_menu('language', $languages_array, $languages_selected, 'onChange="this.form.submit();"') . tep_hide_session_id() . '</form>'; ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
      <td>
<?php

?>

		<table border=1>
		<th class="adminlist"></th>
		<?php 


			// Step 1.
			$td_width = 1000/(count($languages_folder_array));
			
			foreach($languages_folder_array as $lng) {
				echo "<th class='adminlist' width=".$td_width.">".$lng."</th>";
			}
			// Step 2.
			$filepath_language_array= array();
			while (list($key, $files_lng) = each($files_language)){
				//see which file has the most 'define' statements
				// need to loop through all the language files to see the maximum count
				$max_define_count = 0;
				foreach($languages_folder_array as $lng){
					$max_define_count = ($files_lng[$lng]>$max_define_count)?$files_lng[$lng]:$max_define_count;
				}
				
				echo "<tr class='dataTableContent' ><td><b><a href='".tep_href_link(FILENAME_VERIFY_LANGUAGE,"filename=".$key."&directory=".$directory_desc).$query_string_ext."'>".$key."</a></b></td>";
				foreach($languages_folder_array as $lng){
					if ($files_lng[$lng]==$max_define_count){
						echo "<td align=center>".tep_image(DIR_WS_IMAGES . 'icon_check.gif', 'File Good', 16, 16)."</td>";				 
					} elseif (($files_lng[$lng]<$max_define_count) && ($files_lng[$lng]!='')){
						echo "<td align=center>".tep_image(DIR_WS_IMAGES . 'icon_check_incomplete.gif', 'File Missing Vars', 16, 16)."</td>";	
					} else {
						echo "<td align=center><a href='".tep_href_link(FILENAME_VERIFY_LANGUAGE,"filename=".$key."&lang=".$lng."&path=".urlencode($files_lng['path']['english'])."&action=copy")."'>".tep_image(DIR_WS_IMAGES . 'icon_nonexist.gif', 'File Does Not Exist', 16, 16)."</a></td>";	
					}
				}
				echo "</tr>";
					
				// Step 3.
				if($filename!=''){
					$colspan = count($languages_folder_array);
					$filename = ($image_text_filename=='')? $filename:$image_text_filename;
	?>
					<tr class='dataTableRow' align=right>
						<td><a href="<?php echo tep_href_link(FILENAME_VERIFY_LANGUAGE,"directory=".$directory_desc.$query_string_ext); ?>"><< <?php echo TEXT_GO_BACK; ?></a>
						</td>
						<td colspan=<?php echo $colspan;  ?> height=20 border=2><hr></td>
					</tr>
	<?php 			
					while (list($field_name, $fields_lng) = each($files_lng['fields'])){

	?>
						<tr class='dataTableContent'>
							<td><?php echo $field_name;?></td>
	<?php
						foreach($languages_folder_array as $lng){
							$id_base = $field_name.'_'.$lng;
							// See if the field exists or not for the language
	
							//echo __LINE__.DIR_WS_IMAGES .$lng . '/images/buttons/' . $image_file."<br>";
							if ($fields_lng[$lng]!=''){
								// Field Exists
	?>
								<td width=<?php echo $td_width; ?>><div id=<?php echo $id_base."_top";?>>
										<?php echo $fields_lng[$lng];?>
									</div>
									<br>
									<div style='float:right;'>
										<smalltext>
											<a href='#<?php echo $id_base."_top";?>' onclick=javascript:toggle('<?php echo $id_base; ?>')>
												<?php echo tep_image(DIR_WS_IMAGES . 'icon_edit.gif', 'Modify', 16, 16);?>
											</a>
										</smalltext>
									</div>
									<div style='display:none;' id=<?php echo $id_base; ?>>
										<textarea COLS='40' ROWS='10' id=<?php echo $id_base."_text"; ?>>
											<?php echo trim(htmlspecialchars($fields_lng[$lng]));?>
										</textarea>
										<a href='#<?php echo $id_base."_top";?>' onclick=javascript:updateText('<?php echo $field_name."','".$filename."','".$lng."','".urlencode($files_lng['path'][$lng]); ?>')>
											<?php echo tep_image(DIR_WS_IMAGES . 'save_icon.png', 'Save', 16, 16, 'id='.$id_base.'_save');?>
										</a>
										<?php echo tep_image(DIR_WS_IMAGES . 'status_anim.gif', 'Processing', 16, 16, 'id='.$id_base.'_anim style="display:none;"');?>
										<br>
							    			Google Translate
							    		<br>
										<?php echo tep_draw_pull_down_menu('langauge_pair', $google_language_pair_array,$default_language_pair_array[$lng],"id=".$id_base."_select");?>			
										<a href='#<?php echo $id_base."_top";?>' onclick="javascript:getTranslation('<?php echo $field_name."','".$lng;?>');")>
											<?php echo tep_image(DIR_WS_IMAGES . 'icon_upload.gif', 'Translate', 20, 20, 'id='.$id_base.'_translate');?>
										</a>
							    	</div>
							      </td>				 
	<?php
								} else {
									// Field does not exist
									if($files_lng[$lng]!=''){
										//File Exists, but field does not
	?>									<td>
											<div id=<?php echo $id_base."_top";?>>
											</div>
											<br>
											<div style='float:right;'>
												<smalltext>
													<a href='#<?php echo $id_base."_top_".$lng;?>' onclick=javascript:toggle('<?php echo $id_base;?>')>
														<?php echo tep_image(DIR_WS_IMAGES . 'icon_modify.gif', 'Add Field', 16, 16);?>
													</a>
												</smalltext>
											</div>
											<div style='display:none;' id=<?php echo $id_base;?>>
												<textarea COLS='40' ROWS='10' id=<?php echo $id_base."_text";?>>
													<?php echo htmlspecialchars($fields_lng['english']);?>
												</textarea>
												<a href='#<?php echo $id_base."_top";?>' onclick=javascript:updateText('<?php echo $field_name."','".$filename."','".$lng."','".urlencode($files_lng['path'][$lng]);?>')>
													<?php echo tep_image(DIR_WS_IMAGES . 'save_icon.png', 'Save', 16, 16, 'id='.$id_base.'_save');?>
												</a>
												<?php echo tep_image(DIR_WS_IMAGES . 'status_anim.gif', 'Processing', 16, 16, 'id='.$id_base.'_anim style="display:none;"');?>
			           		 					<br>
			           		 					Google Translate
			           		 					<br>
			           		 					<?php echo tep_draw_pull_down_menu('langauge_pair', $google_language_pair_array,$default_language_pair_array[$lng],"id=".$id_base."_select");?>	
			           		 					<a href='#<?php echo $id_base."_top";?>' onclick="javascript:getTranslation('<?php echo $field_name."','".$lng;?>');">
													<?php echo tep_image(DIR_WS_IMAGES . 'icon_upload.gif', 'Translate', 20, 20, 'id='.$id_base.'_translate');?>
												</a>
											  </div>
										</td>	
	<?php 							}else {
										//File does not Exist, it must be created before individual fields can be populated
	?>
										<td>&nbsp</td>
	<?php 
								}
							}
						}
	?>
						</tr>
	<?php 
					}
				}
			}
?>
		</table>
      </td>
      </tr>
    </table></td>
<!-- body_text_eof //-->
  </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
<br>
<?php
  require(DIR_WS_INCLUDES . 'application_bottom.php');

  // -- webMS Modification --
  // reset the Mambo Database connection (stomped by osCommerce Connection)
  if (!DB_SHARED) {
    global $database, $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix;
    $database = new database( $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix );
  }
?>
